package io.kiki.sba.registry.server.clientInterface.remoting.handler;

import io.kiki.sba.registry.api.Channel;
import io.kiki.sba.registry.core.model.RegisterResponse;
import io.kiki.sba.registry.core.model.Scope;
import io.kiki.sba.registry.core.model.SubscriberRegister;
import io.kiki.sba.registry.server.clientInterface.strategy.SubscriberHandlerStrategy;
import io.kiki.sba.registry.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Executor;


public class SubscriberHandler extends AbstractClientDataRequestHandler<SubscriberRegister> {

    @Autowired
    SubscriberHandlerStrategy subscriberHandlerStrategy;

    @Override
    public Object doHandle(Channel channel, SubscriberRegister subscriberRegister) {
        RegisterResponse registerResponse = new RegisterResponse();
        subscriberHandlerStrategy.handleSubscriberRegister(channel, subscriberRegister, registerResponse);
        return registerResponse;
    }

    @Override
    public void checkParam(SubscriberRegister subscriberRegister) {
        if (subscriberRegister.isAcceptMulti()) {
            StringUtil.checkEquals(subscriberRegister.getScope(), Scope.global.name(), "scope");
        }
    }

    @Override
    public Class interest() {
        return SubscriberRegister.class;
    }

    @Override
    public Executor getExecutor() {
        return executorManager.getAccessSubExecutor();
    }
}
